package com.herench.bunch.lab.convert;


import ca.uhn.hl7v2.model.GenericPrimitive;
import ca.uhn.hl7v2.model.Varies;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.message.DSR_Q01;
import ca.uhn.hl7v2.model.v24.segment.MFE;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.model.v24.segment.ODT;
import ca.uhn.hl7v2.model.v24.segment.QRD;
import com.herench.bunch.commons.support.MessageConvert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public abstract class AbstractMessageConvert implements MessageConvert {

    static final Logger logger = LoggerFactory.getLogger(AbstractMessageConvert.class);

    void initResponseMessageMSH(MSH msh) throws Exception {
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("HIS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
    }



    void addGenericPrimitive(MFE mfe, String key, String value) throws Exception {
        GenericPrimitive primitive = new GenericPrimitive(mfe.getMessage());
        StringBuffer buffer = new StringBuffer();
        buffer.append(key).append("=").append(value);
        primitive.setValue(buffer.toString());
        Varies varies = mfe.insertPrimaryKeyValueMFE(mfe.getPrimaryKeyValueMFEReps());
        varies.setData(primitive);
    }


    void addQrd(QRD qrd, String key, String value) throws Exception{
        CE ce =  qrd.insertQrd9_WhatSubjectFilter(qrd.getQrd9_WhatSubjectFilterReps());
        ce.getCe1_Identifier().setValue(key);
        ce.getCe2_Text().setValue(value);
    }

    void addCE(ODT odt, String key, String value) throws Exception{
        CE ce =  odt.insertServicePeriod(odt.getServicePeriodReps());
        ce.getCe1_Identifier().setValue(key);
        ce.getCe2_Text().setValue(value);
    }

    class QdbParamsHelper {
        private List<QdbParam> params;

        public QdbParamsHelper() {
            this.params = new ArrayList<>();
        }

        public List<QdbParam> getParams() {
            return params;
        }

        public void addParams(String key, String value) {
            QdbParam qdbParam = new QdbParam(key, value);
            params.add(qdbParam);
        }


        @Override
        public String toString() {
            StringBuffer buffer = new StringBuffer();
            int index = 0;
            for (QdbParam param : params) {
                buffer.append(param);
                if (!(index == (params.size() - 1))) {
                    buffer.append("#");
                }
                index++;
            }
            return buffer.toString();
        }
    }


    class QdbParam {
        String key;
        String value;

        public QdbParam() {
        }

        public QdbParam(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }


        @Override
        public String toString() {
            return getKey() + "=" + getValue();
        }
    }




}
